package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.TransferAccountHistory;
import cn.net.shuxing.yifu.common.bean.UserInfo;
import cn.net.shuxing.yifu.common.mapper.TransferAccountHistoryMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class TransferAccountHistoryService extends HistoryService<TransferAccountHistory,TransferAccountHistoryMapper> {

    @Autowired
    private UserInfoService userInfoService;

    public List<TransferAccountHistory> getByUserId(Integer userId)
    {
        CommonExample commonExample = new CommonExample(TransferAccountHistory.class);
        Example.Criteria criteria = commonExample.getCriteria();
        criteria.orEqualTo("offerId",userId)
                .orEqualTo("receiverId",userId);
        List<TransferAccountHistory> transferAccountHistories = mapper.selectByExample(commonExample);
        transferAccountHistories.forEach((x)->{complete(x);});
        return transferAccountHistories;
    }

    @Override
    protected void complete(TransferAccountHistory bean)
    {
        if (bean == null)
            return;
        UserInfo offer = userInfoService.getByPrimaryKey(bean.getOfferId());
        bean.setOffer(offer);

        UserInfo receiver = userInfoService.getByPrimaryKey(bean.getReceiverId());
        bean.setReceiver(receiver);
    }
}
